package com.company.cloud.mall.modules.mgr.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.mall.constant.PayMethodEnum;
import com.company.cloud.mall.constant.ShopConstant.PayActionTypeEnum;
import com.company.cloud.mall.constant.ShopConstant.PayConfirmWay;
import com.company.cloud.mall.modules.app.models.PayResultModel;
import com.company.cloud.mall.modules.app.service.ShopAfterSaleRefundsService;
import com.company.cloud.mall.modules.app.service.ShopAfterSaleReturnsService;
import com.company.cloud.mall.modules.app.service.ShopOnlinePayService;
import com.company.cloud.mall.modules.base.entity.OrderPayment;
import com.company.cloud.mall.modules.base.service.OrderPaymentService;
import com.company.cloud.mall.modules.mgr.service.ShopAfterOrderPayService;
import com.company.cloud.pay.modules.api.constant.PayRefundStateEnum;
import com.company.common.server.constant.IRelBusiType;

@Service
public class ShopAfterOrderPayServiceImpl implements ShopAfterOrderPayService{

	
	@Autowired
	private OrderPaymentService orderPaymentService;
	@Autowired
	private ShopAfterSaleRefundsService shopAfterSaleRefundsService;
	@Autowired
	private ShopAfterSaleReturnsService shopAfterSaleReturnsService;
	@Autowired
	private ShopOnlinePayService shopOnlinePayService;
	
	@Override
	public void queryRefundResult(LoginSimpleUserInfo userInfo) {
		 //todo custno
		 List<OrderPayment> paymentList=orderPaymentService.list(new QueryWrapper<OrderPayment>()
				   .select("pay_sn","payment_channel","biz_order_type","biz_order_sn")
				   .eq("type", PayActionTypeEnum.PA_Refund.getCode())
				   .eq("payment_method", PayMethodEnum.PM_Online_Pay.getCode())
				   .eq("payment_state", PayRefundStateEnum.Refund_Ing.getCode())
				   .in("biz_order_type", IRelBusiType.Order_Returns_ObjType,IRelBusiType.Order_Refunds_ObjType)
				   .groupBy("pay_sn","payment_channel")
				   .last(" LIMIT 0,50 ")
		 );
		 if(!CollectionUtils.isEmpty(paymentList)) {
				for(OrderPayment payment:paymentList) {
					this._thirdRefundResult(payment.getCustNo(), payment.getPaymentChannel(), payment.getPaySn(),
							payment.getBizOrderType(),payment.getBizOrderSn(),userInfo);
				}
			}
	}
	
	
	private void _thirdRefundResult(String companyNo, String paymentChannel,String paySN,String bizType,
			String bizSN,LoginSimpleUserInfo userInfo) {
			PayResultModel payResult=shopOnlinePayService.queryPayRefundResult(companyNo, paySN, paymentChannel);
			if(PayRefundStateEnum.Refund_Ing.getCode().equals(payResult.getState())) { //支付中
				return ;
			}
			if(IRelBusiType.Order_Returns_ObjType.equals(bizType)) {
				shopAfterSaleReturnsService.callbackRefund(bizSN, paySN, payResult.getState(), 
						payResult.getThirdState(),PayConfirmWay.ThirdQuery_Confirm, userInfo);
			}
			if(IRelBusiType.Order_Refunds_ObjType.equals(bizType)) {
				shopAfterSaleRefundsService.callbackRefund(bizSN, paySN, payResult.getState(), 
						payResult.getThirdState(), PayConfirmWay.ThirdQuery_Confirm ,userInfo);
			}
	}
	


	@Override
	public void callback(String paySN,String state, String thirdStatus, LoginSimpleUserInfo userInfo) {
		if(PayRefundStateEnum.Refund_Ing.getCode().equals(state)) { //支付中
			return ;
		}
		OrderPayment orderPayment=orderPaymentService.getOne(new QueryWrapper<OrderPayment>().eq("pay_sn", paySN));
		if(orderPayment==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		
		if(IRelBusiType.Order_Returns_ObjType.equals(orderPayment.getBizOrderType())) {
			shopAfterSaleReturnsService.callbackRefund(orderPayment.getBizOrderSn(), paySN, 
					state, thirdStatus,PayConfirmWay.ThirdCallback_Confirm, userInfo);
		}
		if(IRelBusiType.Order_Refunds_ObjType.equals(orderPayment.getBizOrderType())) {
			shopAfterSaleRefundsService.callbackRefund(orderPayment.getBizOrderSn(), paySN, 
					state, thirdStatus,PayConfirmWay.ThirdCallback_Confirm, userInfo);
		}
		
	}
	
	
	
}
